package com.CardBudgetBook.Common;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.CardBudgetBook.DataBase.BudgetDB;
import com.CardBudgetBook.DataBase.BudgetInfo;

public class CommonUsage {
	final static int YEAR = 0;
	final static int MONTH = 1;
	final static int DAY = 2;
	public static int winX, winY;
	public static ArrayList<BudgetInfo> budgetInfo; 
	
	
	
	public static String getYearFromDate(String date){
		return extractFromDate(date, YEAR);
	}
	
	public static String getMonthFromDate(String date){	
		return extractFromDate(date, MONTH);
	}
	
	public static String getDayFromDate(String date){	
		return extractFromDate(date, DAY);
	}
	
	private static String extractFromDate(String date, int type){
		String splittedDate[] = date.split("."); 
		return splittedDate[type];
	}
	
	public static void initDB(Context context){
				
				BudgetDB bDB = new BudgetDB(context);
				bDB.test();
				budgetInfo = bDB.getAll();
				bDB.close();

				for (BudgetInfo bInfo : budgetInfo) {
					Log.d("DB Contents", bInfo.getDate() + ", " + bInfo.getTime()
							+ ", " + bInfo.getCompany() + ", " + bInfo.getLocation()
							+ ", " + bInfo.getOutlay());
				}
		
	}
	
	
	public static String getCurrentTime(){
		Date date = new Date();
		String strTime = String.format("%02d:%02d:%02d",
				date.getHours(), date.getMinutes(), date.getSeconds());
		return strTime;
	}
	
	public static String getToday(){
		Date date = new Date();
		String strDate = String.format("%04d.%02d.%02d",
				date.getYear() + 1900, date.getMonth()+1, date.getDate(),
				date.getHours(), date.getMinutes(), date.getSeconds());
		return strDate;
	}
	
	public static ArrayList findOutlayByDate(String date){
		ArrayList<Float> tempInfo = new ArrayList<Float>();
		for(BudgetInfo bInfo:budgetInfo){
			if(bInfo.getDate().equals(date)){
				tempInfo.add(bInfo.getOutlay());		}
		}
		return tempInfo;
	}
	
	public static void setDisplaySize(Context context){
		Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
	    winX = display.getWidth();
	    winY = display.getHeight();
	}
	
}
